package org.intertwine.rssbot;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Stian Lindhom
 * Date: 21.feb.2010
 * Time: 14:53:43
 * To change this template use File | Settings | File Templates.
 */
public class RssReader extends TimerTask {

    private static Map<String, SyndEntry> seenUrls = new HashMap<String, SyndEntry>();
    private Date startupTime;
    private NewRSSItemListener nr;
    private String feedUrl;
    private String username;
    private String password;

    public RssReader(Feed f, NewRSSItemListener nr) {
        this.nr = nr;
        this.startupTime = new Date();
        this.feedUrl = f.getFeedUrl();
        this.username = f.getUsername();
        this.password = f.getPassword();
    }

    private void readFeed() {
        try {
            URL feedUrl = new URL(this.feedUrl);
            HttpURLConnection hc = (HttpURLConnection) feedUrl.openConnection();
            if (username != null && password != null) {
                String encoding = new sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes());
                hc.setRequestProperty("Authorization", "Basic " + encoding);
            }

            System.out.println("Reading feed...");
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(hc));
            List entries = feed.getEntries();
            for (Object entry : entries) {
                if (entry instanceof SyndEntry) {
                    SyndEntry e = (SyndEntry)entry;
                    System.out.println("Entry: " + e.getTitle());
                    if (e.getPublishedDate().after(startupTime)) {
                        if (!seenUrls.containsKey(e.getUri())) {
                            System.out.println("New entry: " + e.getTitle());
                            nr.onNewRssItem(e.getTitle());
                            seenUrls.put(e.getUri(), e);
                        }
                    }
                }
            }

        } catch (MalformedURLException e) {
            nr.onFaultyRssFeed("The URL specified for feed " + this.feedUrl + " is invalid.");
        } catch (FeedException e) {
            nr.onFaultyRssFeed("An error occured while processing feed " + this.feedUrl + " see the logfile for details.");
            e.printStackTrace();
        } catch (IOException e) {
            nr.onFaultyRssFeed("An I/O error occured while processing feed " + this.feedUrl + " see the logfile for details.");
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        readFeed();
    }


}
